package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Cart;
import com.example.entity.Goods;
import com.example.mapper.GoodsMapper;
import com.example.service.CartService;
import com.example.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    //避免循环依赖
    @Lazy
    private CartService cartService;

    public IPage<Goods> findPage(Page<Goods> page, String name,Long createUserId) {
        return goodsMapper.findPage(page, name, createUserId);
    }

    public IPage<Goods> pageByCategory(Page<Goods> page, Long id) {
        return goodsMapper.pageByCategory(page, id);
    }

    public List<Goods> recommend() {
        return goodsMapper.getRecommend();
    }

    public List<Goods> sales() {
        return goodsMapper.sales();
    }

    public List<Goods> findAll() {
        return goodsMapper.findAll();
    }

    public void removeCartGoods(Long goodsId) {
        log.info("删除goodsId:{}", goodsId);
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Cart::getGoodsId, goodsId);
        cartService.remove(wrapper);
    }
}
